package org.neptune.service.aop;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author lwl
 * @date 2019/7/23 16:49
 */
@Component
@Scope
@Aspect
public class SeckillActionLockAspect {

	private static final Logger logger = LoggerFactory.getLogger(SeckillActionLockAspect.class);

	private static Lock lock = new ReentrantLock(true);

	@Pointcut("@annotation(org.neptune.service.annotation.SeckillActionLock)")
	public void lockAspect() {
	}

	@Around("lockAspect()")
	public Object serviceAround(ProceedingJoinPoint joinPoint) {
		lock.lock();
		try{
			Object obj = joinPoint.proceed();
			return obj;
		} catch (Throwable e){
			logger.error("aspect method error", e);
		} finally{
			lock.unlock();
		}

		return null;
	}
}
